﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SpeedSearch
{
    public class ListViewPerso : ListView
    {
        public delegate void RefreshDelegateHandler(FileInfos fileInfos, bool realTime = false);
        public delegate void RefreshAllDelegateHandler(IEnumerable<FileInfos> fileInfosList);

        public RefreshDelegateHandler RefreshDelegate;
        public RefreshAllDelegateHandler RefreshAllDelegate;

        public void PersoColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Create an instance of the ColHeader class.
            ColHeader clickedCol = (ColHeader)this.Columns[e.Column];

            // Set the ascending property to sort in the opposite order.
            clickedCol.ascending = !clickedCol.ascending;

            // Get the number of items in the list.
            int numItems = this.Items.Count;

            // Turn off display while data is repoplulated.
            this.BeginUpdate();

            // Populate an ArrayList with a SortWrapper of each list item.
            ArrayList SortArray = new ArrayList();
            for (int i = 0; i < numItems; i++)
            {
                SortArray.Add(new SortWrapper(this.Items[i], e.Column));
            }

            // Sort the elements in the ArrayList using a new instance of the SortComparer
            // class. The parameters are the starting index, the length of the range to sort,
            // and the IComparer implementation to use for comparing elements. Note that
            // the IComparer implementation (SortComparer) requires the sort
            // direction for its constructor; true if ascending, othwise false.
            SortArray.Sort(0, SortArray.Count, new SortWrapper.SortComparer(clickedCol.ascending));

            // Clear the list, and repopulate with the sorted items.
            this.Items.Clear();
            for (int i = 0; i < numItems; i++)
                this.Items.Add(((SortWrapper)SortArray[i]).sortItem);

            // Turn display back on.
            this.EndUpdate();
        }

        public ListViewPerso()
            : base()
        {
            RefreshDelegate = new RefreshDelegateHandler(Refresh);
            RefreshAllDelegate = new RefreshAllDelegateHandler(RefreshAll);
        }

        public void RefreshInvoke(Delegate method, FileInfos fileinfos, bool realtime)
        {
            this.Invoke(method, fileinfos, realtime);
        }

        public void RefreshInvoke(Delegate method, IEnumerable<FileInfos> fileinfoslist)
        {
            this.Invoke(method, fileinfoslist);
        }

        private void Refresh(FileInfos fileInfos, bool realTime = false)
        {
            if (fileInfos.ClearFlag)
                this.Items.Clear();
            else
            {
                var listItem = new ListViewItem(new string[] 
                    {   fileInfos.FileName, 
                        fileInfos.FileExtension,
                        fileInfos.FileRoot,
                        GetNormalizedFileSize(fileInfos.FileSize),
                        String.Format("{0} : {1}", fileInfos.FileLastUpdated.ToShortDateString(), fileInfos.FileLastUpdated.ToShortTimeString())
                    });
                this.Items.Add(listItem);

                if (realTime)
                    this.Refresh();
            }
        }

        private void RefreshAll(IEnumerable<FileInfos> fileInfosList)
        {
            foreach (var item in fileInfosList)
            {
                Refresh(item, false);
            }
            this.Refresh();
        }

        private string GetNormalizedFileSize(double size)
        {
            double k = size / 1024;
            return k.ToString("0");
        }
    }
}
